package pipe;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class Pipe {
  public static void main(String args[])
  {
	  PipedOutputStream pos=new PipedOutputStream();
	  PipedInputStream pis=new PipedInputStream();
	  try {
		pos.connect(pis);
	} catch (IOException e) {
		e.printStackTrace();
	}
	  Producer p=new Producer(pos);
	  Consumer c=new Consumer(pis);
	  p.start();
	  c.start();
  }
}
class Consumer extends Thread{
	private PipedInputStream pis;
	public Consumer(PipedInputStream pis){
		this.pis=pis;
	}
	public void run()
	{  
	  try {
	    while(true)
	    {	 
		   System.out.println(pis.read());
		   Thread.sleep(200);
	    }
	  } catch (Exception e) {
		  e.printStackTrace();
		}
	}
}
class Producer extends Thread{
	private PipedOutputStream pos;
	public Producer(PipedOutputStream pos)
	{
	  this.pos=pos;
	}
	public void run()
	{
	  int i=0;
	  try {
	    while(true)
	    {
	     pos.write(i);
		 Thread.sleep(200);
		 i++;
	    }
	  }catch (Exception e) {
		e.printStackTrace();
	   }
	}
}
